﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data;
using DeviceInfoManage.BusinessLogicLayer;

public partial class DailyInspectManage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["adminUserName"] == null)
            {
                Response.Redirect("login.aspx");
            }
            if (!string.IsNullOrEmpty(Request.QueryString["type"]))
            {
                hidType.Value = Request.QueryString["type"];
            }
            else
            {
                Response.Write("<script>alert('参数错误!');</script>");
            }
            if (hidType.Value == "2")
            {
                if (Session["adminType"].ToString() != "管理员")
                {
                    Btn_Add.Enabled = false;
                }
                BindData();
                Btn_Add.Text = "更新";
            }
        }
    }

    private void BindData()
    {
        if (!string.IsNullOrEmpty(Request.QueryString["id"]))
        {
            hidId.Value = Request.QueryString["id"];
        }
        DailyInspect di = (new DailyInspect()).GetModel(int.Parse(hidId.Value));
        txtItemNumber.Text = di.ItemNumber;
        ddlDpt.SelectedValue = di.Department;
        if (ddlMaintPerson.Items.FindByValue(di.Principal) != null)
        {
            ddlMaintPerson.SelectedValue = di.Principal;
        }
        txtDate.Text = di.Date.ToString("yyyy-MM-dd");
        //ddlErrType.SelectedValue = di.ErrType;
        if (di.IsSolve == "0") { cbisSolve.Checked = false; }
        txtErrorReason.Text = di.ErrReason;
        txtErrSolve.Text = di.ErrSolve;

        DataSet ds = (new DailyItem()).GetList("IiD = '" + hidId.Value + "'");
        if (ds.Tables[0].Rows.Count != 0)
        {
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                if (string.IsNullOrEmpty(dr["DatePart"].ToString()))
                {
                    string item = "cb" + dr["DeviceType"].ToString();
                    HtmlInputCheckBox hicb = FindControl(item) as HtmlInputCheckBox;
                    hicb.Checked = true;
                }
                else
                {
                    HtmlInputText hit = FindControl("txt" + dr["DeviceType"].ToString().Trim() + "6") as HtmlInputText;
                    hit.Value = dr["DatePart"].ToString();
                }
            }
        }
    }

    /*添加设备信息*/
    protected void Btn_Add_Click(object sender, EventArgs e)
    {
        DailyInspect di = new DailyInspect();
        di.ItemNumber = txtItemNumber.Text;
        di.Department = ddlDpt.SelectedValue;
        di.Principal = ddlMaintPerson.SelectedValue;
        di.Date = Convert.ToDateTime(txtDate.Text);
        //di.ErrType = ddlErrType.SelectedValue;
        di.State = "0";
        if (cbisSolve.Checked == true) { di.IsSolve = "1"; } else { di.IsSolve = "0"; }
        di.ErrReason = txtErrorReason.Text.Trim();
        di.ErrSolve = txtErrSolve.Text.Trim();
        if (hidId.Value == "")
        {
            int i = di.Add(di);
            if (i != -1)
            {
                hidId.Value = i.ToString();
                Response.Write("<script>alert('添加成功!');location.href=location.href;</script>");
            }
            else
                Response.Write("<script>alert('添加失败!');</script>");
        }
        else
        {
            di.ID = int.Parse(hidId.Value);
            di.Update(di);
            Response.Write("<script>alert('更新成功!');location.href=location.href;</script>");
        }
        if (!string.IsNullOrEmpty(hidItem.Value))
        {
            (new DailyItem()).WriteItem(new string[] { hidId.Value, hidName.Value, hidItem.Value });
        }
    }
}
